package per.dg.socketclient;

import android.os.HandlerThread;
import android.util.Log;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

public class SocketClienThread extends Thread {

    private static final String TAG = "SocketClienThread";

    //定义一个Socket对象
    Socket socket = null;

    private String host;
    private int port;

    boolean threadRunning = true;

    List<String> list;

    public SocketClienThread(String host, int port) {
        this.host = host;
        this.port = port;
        //需要服务器的IP地址和端口号，才能获得正确的Socket对象
//        if(socket == null) {
//            new Thread(new Runnable() {
//                @Override
//                public void run() {
//                    try {
//                        socket = new Socket(host, port);
//                    } catch (UnknownHostException e) {
//                        e.printStackTrace();
//                    } catch (IOException e) {
//                        e.printStackTrace();
//                    }
//                }
//            }).start();
//        }
        list = new ArrayList<>();
    }

    @Override
    public void run() {
        try {
            while (threadRunning){
                if(socket == null){
                    creatSocket();
                }
                if(list.size() == 0){

                }else {
                    synchronized (this){
                        while (socket == null){

                        }
                    }
                    Log.e(TAG, "run: "+"客户端正在发送消息" );
                    OutputStream outputStream = socket.getOutputStream();
                    outputStream.write(list.get(0).getBytes());
                    outputStream.flush();
                    Log.e(TAG, "run: "+"客户端发送消息"+list.get(0) );
                    list.remove(0);
                    Log.e(TAG, "run: "+"客户端发送消息结束" );

                    Log.e(TAG, "run: "+"客户端正在接收消息" );
                    InputStream inputStream = socket.getInputStream();
                    byte[] buf = new byte[1024];
                    int len = 0;
                    while ((len = inputStream.read(buf)) != -1) {
                        Log.e(TAG, "run: " + new String(buf, 0, len) );
                    }
                    Log.e(TAG, "run: "+"客户端接收消息结束" );
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
//        //客户端一连接就可以写数据个服务器了
//        new sendMessThread().start();
//        super.run();
//        try {
//            // 读Sock里面的数据
//            InputStream s = socket.getInputStream();
//            byte[] buf = new byte[1024];
//            int len = 0;
//            while ((len = s.read(buf)) != -1) {
//                System.out.println(new String(buf, 0, len));
//            }
//
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
    }

    private void creatSocket(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    if(socket == null){
                        synchronized (this){
                            if(socket == null){
                                socket = new Socket(host, port);
                                Log.e(TAG, "run: "+"客户端连接服务器成功" );
                            }
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    Log.e(TAG, "run: "+"客户端连接服务器失败" );
                }
            }
        }).start();
    }

    public void addSendMessage(String message){
        list.add(message);
    }

}
